<?php
namespace app\common\model;

class ShopCate extends Common
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    /**
     * 店铺表表关联
     * @return [type] [description]
     */
    public function shop(){
        return $this->hasOne(Shop::class,'id','shop_id');
    }
    /**
     * 删除分类
     */
    public function delCate($params){
        if(!empty($params['id'])){
            $goods_nums=Goods::where('shop_cat_id',$params['id'])->count();
            if($goods_nums>0){
                return [false,'请移除该分类下的商品'];
            }
            $cate=$this->where('id',$params['id'])->find();
            if(!$cate){
                return [false,'分类已经删除'];
            }
            $res=$cate->delete();
            if(!$res){
                return [false,'分类删除失败'];
            }
            return [true,'分类删除成功'];
        }
        return [true,'分类删除失败'];
    }
    /**
     * 更新或修改分类
     */
    public function updateCate($params){
        if(empty($params['id'])){
            $result=$this->create($params,true);
        }else{
            $result=$this->save($params,['id'=>$params['id']]);
        }
        if(!$result){
            return [false,'保存失败'];
        }
        return [true,'保存成功'];
    }
    /**
     * 根据输入的查询条件，返回所需要的where
     * @author sin
     * @param $post
     * @return mixed
     */
    protected function tableWhere($post)
    {
        $where=[];
        if(isset($post['pid']) && $post['pid']!=''){
            $where[]=['pid','=',$post['pid']];
        }
        if(isset($post['shop_id']) && $post['shop_id']!=''){
            $where[]=['shop_id','=',$post['shop_id']];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = [];
        return $result;
    }
    /**
     * 获取分类
     */
    public function getAllList($params){
        return $this->where(['shop_id'=>$params['shop_id'],'status'=>1])->order('indexid asc')->select();
    }
    /**
     * 获取分类
     */
    public function getList($params){
        $list=$this->where('shop_id',$params['shop_id']);
        $total=$list->count();
        $result=$list->page($params['page'],$params['limit'])->order('indexid asc')->select();
        return [true,['list'=>$result,'total'=>$total]];
    }
}
